﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Utilities;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Data;

namespace NumberToTextField
{
    class NumberToTextFieldControl : BaseFieldControl
    {
        protected Label numberAsTextControl;

        protected override void CreateChildControls()
        {
            if (Field == null)
            {
                return;
            }

            base.CreateChildControls();

            numberAsTextControl = new Label();
            numberAsTextControl.ID = ID = "NumberAsTextControl";

            if (ControlMode != SPControlMode.Display)
            {
            }

            if (ControlMode == SPControlMode.Display)
            {
                numberAsTextControl.Text = GetNumberInTextualForm();
            }

           
            

            
            
            Controls.Add(numberAsTextControl);
            
        }
        
        protected override void RenderFieldForDisplay(HtmlTextWriter output)
        {
            output.Write(GetNumberInTextualForm());
            base.RenderFieldForDisplay(output);
        }
 

        public override object Value
        {
            get
            {
                EnsureChildControls();
                return numberAsTextControl.Text;
            }
            set
            {
               
                EnsureChildControls();
                numberAsTextControl.Text = value as string;
            }
        }


        private string GetNumberInTextualForm()
        {

            string numberAsText;
            NumberToTextField myField = Field as NumberToTextField;
            string quantityColumn = myField.QuantityColumn;

            if (string.IsNullOrEmpty(quantityColumn) == true)
            {
                numberAsText = "Please configure the numeric column in the column configuration screen.";
            }
            else
            {
                NumbertoTextBase ns;
                if (myField.LocaleColumn != null )
                { 
                 if (myField.LocaleColumn.Trim() == "")
                    ns = Factory.createinstance(); //(INumbertoText ) new Numeric_String();
                else 
                    ns = Factory.createinstance(new System.Globalization.CultureInfo ( myField.LocaleColumn));
                }

                else
                    ns = Factory.createinstance(); //(INumbertoText ) new Numeric_String();
                
                                   
                if (this.ListItem[quantityColumn] == null || string.IsNullOrEmpty(this.ListItem[quantityColumn].ToString()) == true)
                {
                    numberAsText = "There is no value in the numeric column.";
                }
                else
                {
                    int numericValue = 0;
                    bool convertStatus = int.TryParse(this.ListItem[quantityColumn].ToString(), out numericValue);
                    if (convertStatus == true)
                    {
                        numberAsText = ns.parse(numericValue.ToString());
                    }
                    else
                    {
                        numberAsText = "The value stored in the numeric column is not a number.";

                    }
                }
            }
            
            //numberAsText=System.Threading.Thread.CurrentThread.CurrentUICulture.ToString();
            return numberAsText;
        }

    }

}
